#include "sqlite_command_interface.h"

#include <QDebug>

AddPersonCommand::AddPersonCommand(int32_t id, QString name, int32_t age)
    : id_(id), name_(name), age_(age) { }

bool AddPersonCommand::Execute(const QSqlDatabase &database)
{
    QSqlQuery writer(database);
    writer.prepare(R"(insert into person (id, name, age) values (:id, :name, :age))");
    writer.bindValue(":id", id_);
    writer.bindValue(":name", name_);
    writer.bindValue(":age", age_);

    bool is_successed = writer.exec();
    if (!is_successed) qDebug() << writer.lastError();

    return is_successed;
}

UpdatePersonCommand::UpdatePersonCommand(int32_t id, QString name, int32_t age)
    : id_(id), name_(name), age_(age){}

bool UpdatePersonCommand::Execute(const QSqlDatabase &database)
{
    QSqlQuery writer(database);
    writer.prepare(R"(update person set name = :name, age = :age where id = :id)");
    writer.bindValue(":id", id_);
    writer.bindValue(":name", name_);
    writer.bindValue(":age", age_);

    bool is_successed = writer.exec();
    if (!is_successed) qDebug() << writer.lastError();

    return is_successed;
}

InsertPersonCommand::InsertPersonCommand(QString name, int32_t age)
    : name_(name), age_(age) {}

bool InsertPersonCommand::Execute(const QSqlDatabase &database)
{
    QSqlQuery writer(database);
    writer.prepare(R"(insert into person (name, age) values (:name, :age))");
    writer.bindValue(":name", name_);
    writer.bindValue(":age", age_);

    bool is_successed = writer.exec();
    if (!is_successed) qDebug() << writer.lastError();

    return is_successed;
}
